﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Bilyteam_Website
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable tbGalleries = new DataTable();
        DataTable tbTop = new DataTable();
        DataTable tbLogo = new DataTable();
        DataTable tbBottom = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["TopGalleries"] != null)
                {
                    dtlTopGalleries.DataSource = (DataTable)Session["TopGalleries"];
                    dtlTopGalleries.DataBind();

                    if (Session["LogoGalleries"] != null)
                    {
                        dtlLogoGalleries.DataSource = (DataTable)Session["LogoGalleries"];
                        dtlLogoGalleries.DataBind();
                    }

                    if (Session["BottomGalleries"] != null)
                    {
                        dtlBottomGalleries.DataSource = (DataTable)Session["BottomGalleries"];
                        dtlBottomGalleries.DataBind();
                    }
                }
                else
                {
                    BindData();
                }
            }
        }

        private void BindData()
        {
            string sql = "SELECT Images.ImageID, Images.ImageName, Images.Location, Images.Status AS ImagesStatus, Galleries.GalleryID, Galleries.GalleryName, Galleries.GalleryDescription, ";
            sql += "Galleries.Status AS GalleriesStatus, ImageToGallery.ID, ImageToGallery.IDImage, ImageToGallery.IDGallery ";
            sql += "FROM Galleries INNER JOIN ";
            sql += "ImageToGallery ON Galleries.GalleryID = ImageToGallery.IDGallery INNER JOIN ";
            sql += "Images ON ImageToGallery.IDImage = Images.ImageID ";
            sql += "WHERE ImageToGallery.IDGallery = Galleries.GalleryID AND ImageToGallery.IDImage = Images.ImageID AND Galleries.GalleryID = 1 AND Images.Status = 'True' AND Galleries.Status = 'True'";
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(ds, "Galleries");
            tbGalleries = ds.Tables["Galleries"];

            DataColumn ImageID = new DataColumn("ImageID");
            DataColumn ImageName = new DataColumn("ImageName");
            DataColumn Location = new DataColumn("Location");
            DataColumn ImagesStatus = new DataColumn("ImagesStatus");
            DataColumn GalleryID = new DataColumn("GalleryID");
            DataColumn GalleryName = new DataColumn("GalleryName");
            DataColumn GalleryDescription = new DataColumn("GalleryDescription");
            DataColumn GalleriesStatus = new DataColumn("GalleriesStatus");
            DataColumn ID = new DataColumn("ID");
            DataColumn IDImage = new DataColumn("IDImage");
            DataColumn IDGallery = new DataColumn("IDGallery");

            ImageID.DataType = System.Type.GetType("System.Int32");
            ImageName.DataType = System.Type.GetType("System.String");
            Location.DataType = System.Type.GetType("System.String");
            ImagesStatus.DataType = System.Type.GetType("System.Boolean");
            GalleryID.DataType = System.Type.GetType("System.Int32");
            GalleryName.DataType = System.Type.GetType("System.String");
            GalleryDescription.DataType = System.Type.GetType("System.String");
            GalleriesStatus.DataType = System.Type.GetType("System.Boolean");
            ID.DataType = System.Type.GetType("System.Int32");
            IDImage.DataType = System.Type.GetType("System.Int32");
            IDGallery.DataType = System.Type.GetType("System.Int32");

            tbTop.Columns.Add(ImageID);
            tbTop.Columns.Add(ImageName);
            tbTop.Columns.Add(Location);
            tbTop.Columns.Add(ImagesStatus);
            tbTop.Columns.Add(GalleryID);
            tbTop.Columns.Add(GalleryName);
            tbTop.Columns.Add(GalleryDescription);
            tbTop.Columns.Add(GalleriesStatus);
            tbTop.Columns.Add(ID);
            tbTop.Columns.Add(IDImage);
            tbTop.Columns.Add(IDGallery);

            DataColumn ImageIDLogo = new DataColumn("ImageID");
            DataColumn ImageNameLogo = new DataColumn("ImageName");
            DataColumn LocationLogo = new DataColumn("Location");
            DataColumn ImagesStatusLogo = new DataColumn("ImagesStatus");
            DataColumn GalleryIDLogo = new DataColumn("GalleryID");
            DataColumn GalleryNameLogo = new DataColumn("GalleryName");
            DataColumn GalleryDescriptionLogo = new DataColumn("GalleryDescription");
            DataColumn GalleriesStatusLogo = new DataColumn("GalleriesStatus");
            DataColumn IDLogo = new DataColumn("ID");
            DataColumn IDImageLogo = new DataColumn("IDImage");
            DataColumn IDGalleryLogo = new DataColumn("IDGallery");

            ImageIDLogo.DataType = System.Type.GetType("System.Int32");
            ImageNameLogo.DataType = System.Type.GetType("System.String");
            LocationLogo.DataType = System.Type.GetType("System.String");
            ImagesStatusLogo.DataType = System.Type.GetType("System.Boolean");
            GalleryIDLogo.DataType = System.Type.GetType("System.Int32");
            GalleryNameLogo.DataType = System.Type.GetType("System.String");
            GalleryDescriptionLogo.DataType = System.Type.GetType("System.String");
            GalleriesStatusLogo.DataType = System.Type.GetType("System.Boolean");
            IDLogo.DataType = System.Type.GetType("System.Int32");
            IDImageLogo.DataType = System.Type.GetType("System.Int32");
            IDGalleryLogo.DataType = System.Type.GetType("System.Int32");

            tbLogo.Columns.Add(ImageIDLogo);
            tbLogo.Columns.Add(ImageNameLogo);
            tbLogo.Columns.Add(LocationLogo);
            tbLogo.Columns.Add(ImagesStatusLogo);
            tbLogo.Columns.Add(GalleryIDLogo);
            tbLogo.Columns.Add(GalleryNameLogo);
            tbLogo.Columns.Add(GalleryDescriptionLogo);
            tbLogo.Columns.Add(GalleriesStatusLogo);
            tbLogo.Columns.Add(IDLogo);
            tbLogo.Columns.Add(IDImageLogo);
            tbLogo.Columns.Add(IDGalleryLogo);

            DataColumn ImageIDBottom = new DataColumn("ImageID");
            DataColumn ImageNameBottom = new DataColumn("ImageName");
            DataColumn LocationBottom = new DataColumn("Location");
            DataColumn ImagesStatusBottom = new DataColumn("ImagesStatus");
            DataColumn GalleryIDBottom = new DataColumn("GalleryID");
            DataColumn GalleryNameBottom = new DataColumn("GalleryName");
            DataColumn GalleryDescriptionBottom = new DataColumn("GalleryDescription");
            DataColumn GalleriesStatusBottom = new DataColumn("GalleriesStatus");
            DataColumn IDBottom = new DataColumn("ID");
            DataColumn IDImageBottom = new DataColumn("IDImage");
            DataColumn IDGalleryBottom = new DataColumn("IDGallery");

            ImageIDBottom.DataType = System.Type.GetType("System.Int32");
            ImageNameBottom.DataType = System.Type.GetType("System.String");
            LocationBottom.DataType = System.Type.GetType("System.String");
            ImagesStatusBottom.DataType = System.Type.GetType("System.Boolean");
            GalleryIDBottom.DataType = System.Type.GetType("System.Int32");
            GalleryNameBottom.DataType = System.Type.GetType("System.String");
            GalleryDescriptionBottom.DataType = System.Type.GetType("System.String");
            GalleriesStatusBottom.DataType = System.Type.GetType("System.Boolean");
            IDBottom.DataType = System.Type.GetType("System.Int32");
            IDImageBottom.DataType = System.Type.GetType("System.Int32");
            IDGalleryBottom.DataType = System.Type.GetType("System.Int32");

            tbBottom.Columns.Add(ImageIDBottom);
            tbBottom.Columns.Add(ImageNameBottom);
            tbBottom.Columns.Add(LocationBottom);
            tbBottom.Columns.Add(ImagesStatusBottom);
            tbBottom.Columns.Add(GalleryIDBottom);
            tbBottom.Columns.Add(GalleryNameBottom);
            tbBottom.Columns.Add(GalleryDescriptionBottom);
            tbBottom.Columns.Add(GalleriesStatusBottom);
            tbBottom.Columns.Add(IDBottom);
            tbBottom.Columns.Add(IDImageBottom);
            tbBottom.Columns.Add(IDGalleryBottom);

            if (tbGalleries != null)
            {
                for (int i = 0; i < tbGalleries.Rows.Count; i++)
                {
                    if (i < 10)
                    {
                        DataRow row = tbTop.NewRow();
                        row[ImageID] = int.Parse(tbGalleries.Rows[i]["ImageID"].ToString());
                        row[ImageName] = tbGalleries.Rows[i]["ImageName"].ToString();
                        row[Location] = tbGalleries.Rows[i]["Location"].ToString();
                        row[ImagesStatus] = bool.Parse(tbGalleries.Rows[i]["ImagesStatus"].ToString());
                        row[GalleryID] = int.Parse(tbGalleries.Rows[i]["GalleryID"].ToString());
                        row[GalleryName] = tbGalleries.Rows[i]["GalleryName"].ToString();
                        row[GalleryDescription] = tbGalleries.Rows[i]["GalleryDescription"].ToString();
                        row[GalleriesStatus] = bool.Parse(tbGalleries.Rows[i]["GalleriesStatus"].ToString());
                        row[ID] = int.Parse(tbGalleries.Rows[i]["ID"].ToString());
                        row[IDImage] = int.Parse(tbGalleries.Rows[i]["IDImage"].ToString());
                        row[IDGallery] = int.Parse(tbGalleries.Rows[i]["IDGallery"].ToString());
                        tbTop.Rows.Add(row);
                    }
                    else if (i >= 10 && i < 13)
                    {
                        DataRow row = tbLogo.NewRow();
                        row[ImageIDLogo] = int.Parse(tbGalleries.Rows[i]["ImageID"].ToString());
                        row[ImageNameLogo] = tbGalleries.Rows[i]["ImageName"].ToString();
                        row[LocationLogo] = tbGalleries.Rows[i]["Location"].ToString();
                        row[ImagesStatusLogo] = bool.Parse(tbGalleries.Rows[i]["ImagesStatus"].ToString());
                        row[GalleryIDLogo] = int.Parse(tbGalleries.Rows[i]["GalleryID"].ToString());
                        row[GalleryNameLogo] = tbGalleries.Rows[i]["GalleryName"].ToString();
                        row[GalleryDescriptionLogo] = tbGalleries.Rows[i]["GalleryDescription"].ToString();
                        row[GalleriesStatusLogo] = bool.Parse(tbGalleries.Rows[i]["GalleriesStatus"].ToString());
                        row[IDLogo] = int.Parse(tbGalleries.Rows[i]["ID"].ToString());
                        row[IDImageLogo] = int.Parse(tbGalleries.Rows[i]["IDImage"].ToString());
                        row[IDGalleryLogo] = int.Parse(tbGalleries.Rows[i]["IDGallery"].ToString());
                        tbLogo.Rows.Add(row);
                    }
                    else
                    {
                        DataRow row = tbBottom.NewRow();
                        row[ImageIDBottom] = int.Parse(tbGalleries.Rows[i]["ImageID"].ToString());
                        row[ImageNameBottom] = tbGalleries.Rows[i]["ImageName"].ToString();
                        row[LocationBottom] = tbGalleries.Rows[i]["Location"].ToString();
                        row[ImagesStatusBottom] = bool.Parse(tbGalleries.Rows[i]["ImagesStatus"].ToString());
                        row[GalleryIDBottom] = int.Parse(tbGalleries.Rows[i]["GalleryID"].ToString());
                        row[GalleryNameBottom] = tbGalleries.Rows[i]["GalleryName"].ToString();
                        row[GalleryDescriptionBottom] = tbGalleries.Rows[i]["GalleryDescription"].ToString();
                        row[GalleriesStatusBottom] = bool.Parse(tbGalleries.Rows[i]["GalleriesStatus"].ToString());
                        row[IDBottom] = int.Parse(tbGalleries.Rows[i]["ID"].ToString());
                        row[IDImageBottom] = int.Parse(tbGalleries.Rows[i]["IDImage"].ToString());
                        row[IDGalleryBottom] = int.Parse(tbGalleries.Rows[i]["IDGallery"].ToString());
                        tbBottom.Rows.Add(row);
                    }
                }
            }

            Session["TopGalleries"] = tbTop;
            Session["LogoGalleries"] = tbLogo;
            Session["BottomGalleries"] = tbBottom;

            dtlTopGalleries.DataSource = tbTop;
            dtlTopGalleries.DataBind();

            dtlLogoGalleries.DataSource = tbLogo;
            dtlLogoGalleries.DataBind();

            dtlBottomGalleries.DataSource = tbBottom;
            dtlBottomGalleries.DataBind();
        }
    }
}